<!DOCTYPE html>
<?php
require_once("DB_Config.php");
require_once("func.php");
$db_connect = DB_Connection::getInstance();


$district=$_POST['district'];
$city_name=$_POST['city'];
$address=$_POST['addres'];
$entrance_date=$_POST['datetab'];
$num_bedrooms=$_POST['num_of_room'];
$floor=$_POST['floor'];
$square_feet=$_POST['rsize'];
$price=$_POST['price'];
$property=$_POST['property'];
$type=$_POST['type'];

$ad_id=$_POST['ad_id'];
$oldtype=$_POST['oldtype'];
$video = "0";
$picture = "0";

if (isset($_POST['warehouse'])) { $warehouse = $_POST['warehouse']; } 
else { $warehouse = "0"; }
if (isset($_POST['parking'])) { $parking = $_POST['parking']; } 
else { $parking = "0"; }
if (isset($_POST['elevator'])) { $elevator = $_POST['elevator']; } 
else { $elevator = "0"; }
if (isset($_POST['air_conditioner'])) { $air_conditioner = $_POST['air_conditioner']; } 
else { $air_conditioner = "0"; }

echo   $district ."<br>";
echo   $city_name ."<br>";
echo   $address ."<br>";
echo   $entrance_date."<br>";
echo   $num_bedrooms ."<br>";
echo   $floor ."<br>";
echo   $square_feet ."<br>";
echo   $price."<br>";
echo   $property ."<br>";
echo   $type ."<br>";
echo   $warehouse."<br>";
echo   $parking ."<br>";
echo   $elevator ."<br>";
echo   $air_conditioner ."<br>";
echo   $ad_id ."<br>";
echo   $oldtype ."<br>";


if($type!=$oldtype){

//if($oldtype == "sellhouse"){
// $sql = "DELETE FROM sellhouse(district, city_name, address, property, square_feet, floor, num_bedrooms, warehouse,
// parking, air_conditioner, elevator, upload_date, bump_date, price, username, entrance_date, longitude,
// latitude, comments, ad_id, video, picture) WHERE ad_id==$ad_id;
// }

if ($oldtype == "partner"){ $sql="DELETE FROM partnerhouse WHERE ad_id=$ad_id";}
if ($oldtype == "rent"){ $sql="DELETE FROM renthouse WHERE ad_id=$ad_id";}
if ($oldtype == "sell"){ $sql="DELETE FROM sellhouse WHERE ad_id=$ad_id";}

$stmt = $db_connect->db_conn->prepare($sql);
$param = array($ad_id);
if ($stmt->execute($param)){
	echo $ad_id." have been deleted<br>";
}
else{
	echo $ad_id;
	echo "&nbspuser havent been deleted - ERROR<br>";
}



if($type == "sellhouse"){
$sql = "INSERT INTO sellhouse(district, city_name, address, property, square_feet, floor, num_bedrooms, warehouse,
  parking, air_conditioner, elevator, upload_date, bump_date, price, username, entrance_date, longitude,
  latitude, comments, ad_id, video, picture)
            VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        $stmt = $db_connect->db_conn->prepare($sql);
        $param=array($district,$city_name , $address, $property, $square_feet, $floor, $num_bedrooms, $warehouse,
          $parking, $air_conditioner, $elevator, $upload_date, $bump_date, $price, $username, $entrance_date, $longitude,
          $latitude, $comments, $ad_id, $video, $picture);
}

if($type == "renthouse"){
$sql = "INSERT INTO renthouse(district, city_name, address, property, square_feet, floor, num_bedrooms, warehouse,
  parking, air_conditioner, elevator, upload_date, bump_date, price, username, entrance_date, longitude,
  latitude, comments, ad_id, video, picture)
            VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        $stmt = $db_connect->db_conn->prepare($sql);
        $param=array($district,$city_name , $address, $property, $square_feet, $floor, $num_bedrooms, $warehouse,
          $parking, $air_conditioner, $elevator, $upload_date, $bump_date, $price, $username, $entrance_date, $longitude,
          $latitude, $comments, $ad_id, $video, $picture);
}

if($type == "partnerhouse"){
$sql = "INSERT INTO partnerhouse(district, city_name, address, property, square_feet, floor, num_bedrooms, warehouse,
  parking, air_conditioner, elevator, upload_date, bump_date, price, username, entrance_date, longitude,
  latitude, comments, ad_id, video, picture)
            VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        $stmt = $db_connect->db_conn->prepare($sql);
        $param=array($district,$city_name , $address, $property, $square_feet, $floor, $num_bedrooms, $warehouse,
          $parking, $air_conditioner, $elevator, $upload_date, $bump_date, $price, $username, $entrance_date, $longitude,
          $latitude, $comments, $ad_id, $video, $picture);
}

if($stmt->execute($param)){
echo "Done";

      }

 }

?>